package org.cn.cf.db;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConBeanFactory {
	private static final Log log = LogFactory.getLog(ConBeanFactory.class);
	private static final long lifeTimes = 1000*60*5;//FIXME use this value,decide a ConnectBean is expire.
	private static int defineSize = 2;// the default pool size.
	
	public static ConnectBean getConBean(){
		if(ConnectionPool.size<1){
			try{
				initConPool();
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
		}
		return ConnectionPool.getConBean();
	}
	
	public static void initConPool() throws Exception{
		String drive = System.getProperty("CF_DB_DRIVER");
		String url = System.getProperty("CF_CON_URL");
		String user = System.getProperty("CF_USER");
		String password = System.getProperty("CF_PASSWORD");	

		try{
			defineSize = Integer.parseInt(System.getProperty("CF_POOLSIZE"));
		}catch(Exception e){
			log.info("The pool size is not seted,will use the default pool size 2");
		}
		
		initConPool(drive,url,user,password,defineSize);
	}
	
	public static void initConPool(String drive,String url,String user,String password,int size)throws Exception{

		ConnectionPool pool = new ConnectionPool();
		pool.init(drive,url,user,password,size);
		pool.loadPool();
	}
	
	public static void releaseConPool(){
		ConnectionPool.closePool();
	}
	
}
